package com.csdj.crm.controller;

import com.csdj.crm.base.BaseController;
import com.csdj.crm.base.ResultInfo;
import com.csdj.crm.exceptions.ParamsException;
import com.csdj.crm.model.UserModel;
import com.csdj.crm.service.UserService;
import com.csdj.crm.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * 用户模块 controller层
 */
@Controller
@RequestMapping("user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    /**
     * 用户登录
     *
     * @param userName
     * @param userPwd
     * @return
     */
    @PostMapping("login")
    @ResponseBody
    public ResultInfo userLogin(String userName, String userPwd) {
        //通过try-cath 捕获service异常
        //如果service抛出异常则表示登录失败  否则登录成功
        ResultInfo resultInfo = new ResultInfo();

        //调用service层的登录方法
        UserModel userModel = userService.userLogin(userName, userPwd);
        //设置resultInfo的result值 (将数据返回给请求)
        System.out.println(userModel);
        resultInfo.setResult(userModel);
       /* try {
            //调用service层的登录方法
            UserModel userModel = userService.userLogin(userName, userPwd);
            //设置resultInfo的result值 (将数据返回给请求)
            System.out.println(userModel);
            resultInfo.setResult(userModel);

        } catch (ParamsException p) {
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        } catch (Exception e) {
            resultInfo.setCode(500);
            resultInfo.setMsg("登录失败！");
        }*/

        return resultInfo;
    }


    /**
     * 修改用户密码
     *
     * @param request
     * @param oldPassword
     * @param newPassword
     * @param repeatPassword
     * @return
     */
    @PostMapping("updatePwd")
    @ResponseBody
    public ResultInfo updateUserPassword(HttpServletRequest request,
                                         String oldPassword,
                                         String newPassword,
                                         String repeatPassword) {
        ResultInfo resultInfo = new ResultInfo();

        try {
            //获取cookie中的userid
           Integer userId= LoginUserUtil.releaseUserIdFromCookie(request);
           //调用Service层修改密码方法
           userService.updatePassword(userId,oldPassword,newPassword,repeatPassword);

        } catch (ParamsException p) {
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            resultInfo.setCode(500);
            resultInfo.setMsg("修改密码失败!");
            e.printStackTrace();
        }
        return resultInfo;
    }

    @RequestMapping("toPasswordPage")
    public String toPasswordPage(){

        return "user/password";
    }

    /**
     *查询所有的销售人员
     * @return
     */
    @RequestMapping("queryAllSales")
    @ResponseBody
    public List<Map<String,Object>> queryAllSales(){

        return userService.queryAllSales();
    }


}
